package com.aios.action.settings.switches;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.aios.entity.Switch;
import com.aios.entity.SwitchPanel;
import com.aios.service.SwitchPanelService;
import com.aios.service.SwitchService;

public class SwitchesListAction extends AbstractSwitchesAction {

	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_SWITCH_PANEL_SIZE = 1;

	private List<Switch> switchList;
	private List<SwitchPanel> switchPanelList;
	private String panelId;
	private Switch switchButton;
	private String userId;

	@Qualifier("switchPanelService")
	@Autowired
	protected SwitchPanelService switchPanelService;
	@Autowired
	protected SwitchService switchService;

	protected void prepare() {
		switchButton = switchService.findById(id);
	}

	public String execute() {
		try {
			if (id != null) {
				switchList = switchService.findBySwitchPanel(id);
			} else {
				switchPanelList = switchPanelService
						.findByUserId(getSessionUserVariable().getId());

				if (switchPanelList == null
						|| switchPanelList.size() < DEFAULT_SWITCH_PANEL_SIZE) {
					//actionError = getText("switch.error.panel.not.exist");
					//return INPUT;
				} else if (switchPanelList.size() == DEFAULT_SWITCH_PANEL_SIZE) {
					panelId = switchPanelList.get(0).getId();
					switchList = switchService.findAllBySwitchPanelId(panelId);

				} else if (switchPanelList.size() > DEFAULT_SWITCH_PANEL_SIZE) {
					// [TODO] right now the system only supports for single
					// switch panel
					panelId = switchPanelList.get(0).getId();
					switchList = switchService.findAllBySwitchPanelId(panelId);
					// use when multiple switch panels available for one user.
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return SUCCESS;
	}

	public List<Switch> getSwitchList() {
		return switchList;
	}

	public void setSwitchList(List<Switch> switchList) {
		this.switchList = switchList;
	}

	public List<SwitchPanel> getSwitchPanelList() {
		return switchPanelList;
	}

	public void setSwitchPanelList(List<SwitchPanel> switchPanelList) {
		this.switchPanelList = switchPanelList;
	}

	public String getPanelId() {
		return panelId;
	}

	public void setPanelId(String panelId) {
		this.panelId = panelId;
	}

	public Switch getSwitchButton() {
		return switchButton;
	}

	public void setSwitchButton(Switch switchButton) {
		this.switchButton = switchButton;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}
}
